#include "CSwscale.h"



CSwscale::CSwscale(AVPixelFormat src_pixfmt, int src_w, int src_h, AVPixelFormat dst_pixfmt, int dst_w, int dst_h)
{
	m_src_pixfmt = src_pixfmt;
	m_src_w = src_w;
	m_src_h = src_h;

	m_dst_pixfmt = dst_pixfmt;
	m_dst_w = dst_w;
	m_dst_h = dst_h;

	m_img_convert_ctx = sws_alloc_context();
	//Set Value
	av_opt_set_int(m_img_convert_ctx, "sws_flags", SWS_BICUBIC | SWS_PRINT_INFO, NULL);
	av_opt_set_int(m_img_convert_ctx, "srcw", src_w, NULL);
	av_opt_set_int(m_img_convert_ctx, "srch", src_h, NULL);
	av_opt_set_int(m_img_convert_ctx, "src_format", src_pixfmt, NULL);
	//'0' for MPEG (Y:0-235);'1' for JPEG (Y:0-255)
	av_opt_set_int(m_img_convert_ctx, "src_range", 1, NULL);
	av_opt_set_int(m_img_convert_ctx, "dstw", dst_w, NULL);
	av_opt_set_int(m_img_convert_ctx, "dsth", dst_h, NULL);
	av_opt_set_int(m_img_convert_ctx, "dst_format", dst_pixfmt, NULL);
	av_opt_set_int(m_img_convert_ctx, "dst_range", 1, NULL);
	sws_init_context(m_img_convert_ctx, NULL, NULL);

	

}


CSwscale::~CSwscale()
{
	sws_freeContext(m_img_convert_ctx);
}

int CSwscale::scale(const uint8_t * const srcSlice[], const int srcStride[], uint8_t * const dst[], const int dstStride[])
{
	return sws_scale(m_img_convert_ctx, srcSlice, srcStride, 0, m_src_h, dst, dstStride);
}
